# Experimental kernel makefile

CC  = gcc
CCP = g++
AS  = nasm
LD  = ld
AR  = ar
SED = sed
PL = perl

ifndef BASE
	BASE = ../
endif

INCL = \
	-I$(BASE)/ \
	-I$(BASE)/kernel \
	-I$(BASE)/kernel/klibc/include \
	-I$(BASE)/kernel/include \
	-include debug.h \
	-include kconfig.h \

LIBC_PATH = $(BASE)/kernel/klibc

LIBACPI_PATH = $(BASE)/kernel/acpica
	
LIB_PATH = \
	-L$(LIBC_PATH) \
	-L$(LIBACPI_PATH) \
	
LIBS = \
	-lklibc \
	-lacpica \

DEFINES = \
	-DMONARC \
#	-D__DEBUG \

OPT_OR_DEBUG = \
	-O1 -fno-rtti -fno-exceptions \
#	-g

C_OPT = -Wall $(OPT_OR_DEBUG) -mno-stack-arg-probe -ffreestanding -nostartfiles -nodefaultlibs -nostdlib -nostdinc -T kernel.ld $(INCL) $(DEFINES)
OC_OPT = -Wall $(OPT_OR_DEBUG) -mno-stack-arg-probe -ffreestanding -nostartfiles -nodefaultlibs -nostdlib -nostdinc $(INCL) $(DEFINES)

CC_OPT = -Wall $(OPT_OR_DEBUG) -mno-stack-arg-probe -nostartfiles -fno-builtin -nodefaultlibs -nostdlib -nostdinc -T kernel.ld $(INCL) $(DEFINES)
OCC_OPT = -Wall $(OPT_OR_DEBUG) -fpermissive -mno-stack-arg-probe -nostartfiles -fno-builtin -nodefaultlibs -nostdlib -nostdinc $(INCL) $(DEFINES)

#CC_OPT = -Wall $(OPT_OR_DEBUG) -mno-stack-arg-probe -nostartfiles -nodefaultlibs -nostdlib -nostdinc -T kernel.ld $(INCL) $(DEFINES)
#OCC_OPT = -Wall $(OPT_OR_DEBUG) -mno-stack-arg-probe -nostartfiles -nodefaultlibs -nostdlib -nostdinc $(INCL) $(DEFINES)


MALLOC_OPTS = -Wall $(OPT_OR_DEBUG) -mno-stack-arg-probe -ffreestanding -nostartfiles -nodefaultlibs -nostdlib -nostdinc $(INCL) -include include/malloc-fix.h
KMALLOC_OPTS = -Wall $(OPT_OR_DEBUG) -mno-stack-arg-probe -ffreestanding -nostartfiles -nodefaultlibs -nostdlib -nostdinc $(INCL) -include include/malloc-fix.h -DKERNEL

MKDIR = mkdir
CP = cp
CAT = cat
RM = rm -f

SYS_MODULES = \
	modules/i8042/module.o \
	modules/serial/module.o \
	modules/display/module.o \
	../services/test/service.lzss \
#	modules/threadtest/module.o \
#	modules/pcitests/module.o \
#	modules/pcnetpro/module.o \
#	modules/vm86test/module.o \
#	modules/ide/module.o \

KERNEL_MODULES = \
	cxa_abi.o \
	new.o \
	malloc.o \
	hash.o \
	lzss.o \
	pecoff.o \
	Namespace.o \
	Log.o \
	Process.o \
	Thread.o \
	Queue.o \
	Message.o \
	KernelSymbols.o \
	AddressSpace.o \
	Page.o \
	Cache.o \
	platform/c_startup.o \
	platform/panic.o \
	platform/fpu.o \
	platform/atomic.o \
	platform/udivmoddi3.o \
	platform/spinlock.o \
	platform/mutex.o \
	platform/Platform.o \
	platform/Descriptor.o \
	platform/Cpu.o \
	platform/Memory.o \
	platform/Interrupt.o \
	platform/Pic.o \
	platform/Pit.o \
	platform/Timer.o \
	platform/Pci.o \
	platform/PageMap.o \
	platform/PageTable.o \
	platform/RegUtil.o \
	platform/syscall.o \
	platform/Acpi.o \

#	HandleMap.o \

all: disk.flp

clean:
	$(RM) kernel.exe kernel.bin bootsector.bin disk.flp *.o platform/*.o

disk.flp: pm_bload.bin kernel.bin
	dd if=pm_bload.bin of=disk.flp bs=512 count=2
	dd if=kernel.bin of=disk.flp bs=512 seek=2

pm_bload.bin : pm_bload.asm pm_bload.pl kernel.bin
	$(PL) pm_bload.pl pm_bload.asm > pm_bload.fix.asm
	$(AS) -fbin pm_bload.fix.asm -o pm_bload.bin

kernel.bin: kernel.exe
	objcopy -O binary -R .bss kernel.exe kernel.bin

kernel.exe : kernel.cc pm_system.o $(KERNEL_MODULES) $(SYS_MODULES) $(LIBC_PATH)/libklibc.a $(LIBACPI_PATH)/libacpica.a
	$(CC) $(CC_OPT) kernel.cc $(KERNEL_MODULES) $(LIB_PATH) -Wl,--start-group $(LIBS) -Wl,--end-group -Wl,-Map,kernel.map -o kernel.exe
	objdump -t kernel.exe | perl symbols.pl > kernel.sym
	
pm_system.o : platform/pm_system.asm
	$(AS) -fwin32 platform/pm_system.asm -o pm_system.o

malloc.o: malloc.c include/malloc-fix.h
	$(CC) $(MALLOC_OPTS) -c $< -o $@

# temporary dep so that Platform.cc gets recompiled whenever a module is
# changed
platform/Platform.o: platform/Platform.cc $(SYS_MODULES)
	$(CC) $(OCC_OPT) -c $< -o $@

#services.o: services.cc $(SERVICES)
#	$(CC) $(OCC_OPT) -c $< -o $@

%.o : %.asm
	$(AS) -O99 -fwin32 $< -o $@

%.o: %.c %.h
	$(CC) $(OC_OPT) -c $< -o $@
	
%.o: %.c
	$(CC) $(OC_OPT) -c $< -o $@

%.o: %.cc %.h
	$(CCP) $(OCC_OPT) -c $< -o $@

%.o: %.cc
	$(CCP) $(OCC_OPT) -c $< -o $@

